package cn.klandor.picasa.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

import cn.klandor.picasa.util.GPicasa;
import cn.klandor.picasa.util.ImageFileFilter;
import cn.klandor.picasa.util.Photo;
import cn.klandor.picasa.util.PhotoLoadingWorker;




public class GPicasaFrame extends JFrame {
	
	private static final long serialVersionUID = 1L;
	
	private PhotoPanel pp;
	private PhotoChooser pc;
	private ControlPanel cp;
	private ArrayList <Photo> listPhotos;
	BufferedImage imgIcon;
	BufferedImage img;
	File[] images ;
	LoadingPanel loadingPanel;
	
	public GPicasaFrame(String title){
		super(title);
		try {
			init();
		} catch (Exception e) {
			e.printStackTrace();
		}
		setIconImage(new ImageIcon(getClass().getResource("/res/g.png")).getImage());
		setMinimumSize(new Dimension(400,300));
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	private void init() throws Exception {
				
		pp = new PhotoPanel();
		pc = new PhotoChooser(pp);
		cp= new ControlPanel(pc,pp);
		cp.addLoadAction(new LoadPhotoAction());
		
		JPanel panel = new JPanel(new StackLayout());
		panel.add(pp,StackLayout.TOP);
		panel.add(new ChooserPanel(pc),StackLayout.TOP);
		panel.add(cp,StackLayout.TOP);
		
		panel.setBackground(Color.GRAY);
		
		add(panel);
	}
	
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		GPicasaFrame gf = new GPicasaFrame("GPicasa Browser");
		gf.setSize(800, 600);
		gf.setLocationRelativeTo(null);
        gf.setVisible(true);
	}
	
	private class LoadPhotoAction implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			getFiles();
		}
		
	}
	public void getFiles(){
		JFileChooser chooser = new JFileChooser();
		chooser.setFileFilter(new ImageFileFilter());
		chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		int choice = chooser.showOpenDialog(this);
		if (choice == JFileChooser.APPROVE_OPTION) {
			
			File fileDir = chooser.getSelectedFile();
			File file = null;
			if (fileDir.isFile()) {
				file = fileDir;
				fileDir = file.getParentFile();
			}
			
			images = fileDir.listFiles(new ImageFileFilter());
			loadingPanel = new LoadingPanel(images.length);
			this.setGlassPane(loadingPanel);
			loadingPanel.setVisible(true);
			listPhotos = new ArrayList <Photo>();
			PhotoLoadingWorker pwk = new PhotoLoadingWorker(){
				public void startTask() {
					for (int i = 0; i< images.length ; i++) {
						File imgFile=images[i];
						try {
							img = ImageIO.read(imgFile);
							imgIcon =GPicasa.TranslateToFortyIcon(img);
							listPhotos.add( new Photo(
										imgFile, 
										imgIcon,// icon Photo
										imgFile.getName(),
										img.getWidth(),
										img.getHeight()
										));
						} catch (IOException e) {
							e.printStackTrace();
						}
						loadingPanel.setProgress(i+1);
					}
					pc.setListPhotos(listPhotos);
					loadingPanel.setVisible(false);
				}
			};
			pwk.execute();

			
		}
	}
	

}
